package com.yuanqiworld.vw.web;

import com.yuanqiworld.vw.domain.UserInfo;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.roo.addon.web.mvc.controller.finder.RooWebFinder;
import org.springframework.roo.addon.web.mvc.controller.json.RooWebJson;
import org.springframework.roo.addon.web.mvc.controller.scaffold.RooWebScaffold;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

@RequestMapping("/userinfoes")
@Controller
@RooWebFinder
@RooWebScaffold(path = "userinfoes", formBackingObject = UserInfo.class)
@RooWebJson(jsonObject = UserInfo.class)
public class UserInfoController {

    @RequestMapping(method = RequestMethod.PUT, produces = "text/html")
    public String update(@Valid UserInfo userInfo, BindingResult bindingResult, Model uiModel, HttpServletRequest httpServletRequest) {
        if (bindingResult.hasErrors()) {
            populateEditForm(uiModel, userInfo);
            return "userinfoes/update";
        }
        uiModel.asMap().clear();
        userInfo.setPassword(UserInfo.findUserInfo(userInfo.getId()).getPassword());
        userInfo.merge();
        return "redirect:/userinfoes/" + encodeUrlPathSegment(userInfo.getId().toString(), httpServletRequest);
    }

    @RequestMapping(method = RequestMethod.POST, produces = "text/html")
    public String create(@Valid UserInfo userInfo, BindingResult bindingResult, Model uiModel, HttpServletRequest httpServletRequest) {
        if (UserInfo.findUserInfoesByUsername(userInfo.getUsername()).getResultList().size() > 0) {
            bindingResult.rejectValue("username", "username.invalid");
        }
        if (bindingResult.hasErrors()) {
            populateEditForm(uiModel, userInfo);
            return "userinfoes/create";
        }
        uiModel.asMap().clear();
        String password = userInfo.randomPassword();
        userInfo.persist();
        String pathSegment = userInfo.getId().toString();
        return "redirect:/userinfoes/" + encodeUrlPathSegment(pathSegment, httpServletRequest) + "?password=" + password;
    }

    @RequestMapping(value = "/{id}", params = "passwordForm", produces = "text/html")
    public String updatePasswordForm(@PathVariable("id") Long id, Model uiModel) {
        populateEditForm(uiModel, UserInfo.findUserInfo(id));
        return "userinfoes/updatePassword";
    }

    @RequestMapping(method = RequestMethod.PUT, params = "updatePassword", produces = "text/html")
    public String updatePassword(@Valid UserInfo userInfo, @RequestParam(value = "oldPassword", required = false) String oldPassword, @RequestParam(value = "confirmPassword", required = false) String confirmPassword, BindingResult bindingResult, Model uiModel, HttpServletRequest httpServletRequest) {
        UserInfo user = UserInfo.findUserInfo(userInfo.getId());
        if (!UserInfo.isPasswordValid(user.getPassword(), oldPassword)) {
            bindingResult.rejectValue("oldPassword", "error.oldpassword");
        }
        if (userInfo.getPassword() != null) {
            if (userInfo.getPassword().equals(confirmPassword)) {
                bindingResult.rejectValue("confirmPassword", "error.confirmpassword");
            }
        }
        if (bindingResult.hasErrors()) {
            populateEditForm(uiModel, userInfo);
            return "userinfoes/updatePassword";
        }
        uiModel.asMap().clear();
        user.setPassword(userInfo.getPassword());
        user.encodePassword();
        user.merge();
        return "redirect:/";
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.PUT, params = "initPassword", produces = "text/html")
    public String initPassword(@PathVariable("id") Long id, HttpServletRequest httpServletRequest) {
        UserInfo userInfo = UserInfo.findUserInfo(id);
        String password = userInfo.randomPassword();
        userInfo.merge();
        String pathSegment = userInfo.getId().toString();
        return "redirect:/userinfoes/" + encodeUrlPathSegment(pathSegment, httpServletRequest) + "?password=" + password;
    }

    @RequestMapping(value = "/{id}", produces = "text/html")
    public String show(@PathVariable("id") Long id, @RequestParam(value = "password", required = false) String password, Model uiModel) {
        UserInfo userInfo = UserInfo.findUserInfo(id);
        userInfo.setPassword(password);
        uiModel.addAttribute("userinfo", userInfo);
        uiModel.addAttribute("itemId", id);
        return "userinfoes/show";
    }
}
